/*
 * Valentine Gladstein
 * SOLAR ID 108731583
 * Homework #6
 * CSE214
 * Recitation 03-Phil Ammirato
 * Grading TA: Yanjie Li
 */

import java.io.*;
import java.util.*;

public class SpamFilter implements Serializable{
    //Works like a spam filter. This class may be serialized so that badWords does not need to be rebuilt with each use


    private Hashtable<String, String> badWords;
    //a HashTable contianing the SpamFilter's blacklisted words 

    public SpamFilter() {
	badWords = new Hashtable<String, String>();
    }


    /**
     * Adds a word to badWords
     * @param word:  The word to be added to badWords
     *
     */
    public void insert(String word) {
	String lowerWord = word.toLowerCase();
	badWords.put(lowerWord, lowerWord);
    }

   /**
     * Removes a word from badWords
     * @param word:  The word to be removed from badWords
     *
     */
    public void remove(String word) throws EntryNotFoundException {
	String lowerWord = word.toLowerCase();
	if (badWords.containsKey(lowerWord)) 
	    badWords.remove(lowerWord);
	else throw new EntryNotFoundException();
    }

   /**
     * checks whether a word is in badWords
     * @param checkMe: The word to be checked
     *
     */
    public boolean isBadWord(String checkMe) {
	String lowerWord = checkMe.toLowerCase();
	return badWords.containsKey(lowerWord);
    }

   /**
     * Checks the number of "bad words" found in a message
     * @param message:  The message to be checked
     * @return : the percentage (as a decimal) of words in @param which are found in badWords
     *
     */
    public float checkEmail(String message) {
	float result = 0;
	String[] words = message.toLowerCase().split(" ");
 
	for (int i = 0 ; i < words.length; i++) {
	    if (badWords.containsKey(words[i]))
		result++; 
	}
	result /= words.length;
	return result;
    }
}
